<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Date: 2022/10/19
 * Time: 10:45
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SaleUser extends Model
{
    use HasFactory;

    protected $table = 'sp_sale_user';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //分销商级别
    const LEVEL_ONE = 1; //总级别
    const LEVEL_TWO = 2;  //一级分销商
    const LEVEL_THREE = 3;  //二级分销商

    //分销商类型
    const SALE_ONLINE = 1; //线上
    const SALE_OFFLINE = 2;  //线下

    //分销商结算类型
    const SATTLE_ONCE = 1; //一次性
    const SATTLE_MORE = 2;  //多次

    //分销商分成类型
    const RATE_RENT = 1; //租金比例结算
    const RATE_FOREGIFT = 2;  //官网价比例结算
    const RATE_MONEY = 3;  //固定金额结算
    const RATE_RENT_MONEY = 4;  //租金比例+固定金额结算
    const RATE_FOREGIFT_MONEY = 5;  //官网价比例+固定金额结算


    public function shop(){
        return $this->belongsTo(ShopUser::class,'shop_user_id','id')->select(['id', 'shopname']);
    }
    //查询分销商信息-单条
    protected function GetSaleUser($where)
    {
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : array();
        // dd($data);
        if($data['rate_type'] == SaleUser::RATE_RENT){
            //租金比例结算
            $data['rate_type_name'] = '租金比例结算('.$data['rate'].'%)';
        }else if($data['rate_type'] == SaleUser::RATE_FOREGIFT){
            //官网价比例结算
            $data['rate_type_name'] = '官网价比例结算('.$data['rate'].'%)';
        }else if($data['rate_type'] == SaleUser::RATE_MONEY){
            //固定金额结算
            $data['rate_type_name'] = '固定金额结算('.$data['rate_money'].'元)';
        }else if($data['rate_type'] == SaleUser::RATE_RENT_MONEY){
            //租金比例+固定金额结算
            $data['rate_type_name'] = '租金比例结算('.$data['rate'].'%)+固定金额结算('.$data['rate_money'].'元)';
        }else if($data['rate_type'] == SaleUser::RATE_FOREGIFT_MONEY){
            //官网价比例+固定金额结算
            $data['rate_type_name'] = '官网价比例结算('.$data['rate'].'%)+固定金额结算('.$data['rate_money'].'元)';
        }else{
            $data['rate_type_name'] = '暂未设置结算类型，请联系管理员处理';
        }
        return $data;
    }
    //查询分销商信息-多条
    protected function GetAllSaleUser($pageSize,$where)
    {
        $data = $this
            ->where($where)
            ->with(['shop'])
            ->orderBy('create_time', 'desc')
            ->orderBy('is_check', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        return $data;
    }
}
